package com.zsrt.highschool.service;



import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import com.zsrt.highschool.entity.Examinee;

public class ExamineeService {
	
	private static final String SAVE_FILE="E:\\eclipse\\lijinhui-highschool\\highschool.bin";

	private List<Examinee> examinees ;

	
	
	public ExamineeService() {
		examinees = new ArrayList<>();
		load();
	}
	
	/**
	 * 添加
	 * @param examinee
	 */
	public void add(Examinee examinee){
		examinees.add(examinee);
	}
	
	/**
	 * 修改
	 * @param examinee
	 * @return
	 */
	public boolean modify(Examinee examinee){
		 
		int index = examinees.indexOf(examinee);
		if(index==-1){
			return false;
		}else{
			examinees.set(index, examinee);
			return true;
		}
	}
	
	/**
	 * 删除
	 * @param examinee
	 * @return
	 */
	public boolean delete(String ticketId){
		Examinee examinee = getExamineeByTicketId(ticketId);
		return examinees.remove(examinee);
	}
	/**
	 * 根据户籍所在地查询
	 * @param ticketId
	 * @return
	 */
	public List<Examinee> findByCity(String city){
		List<Examinee>list = new ArrayList<>();
		for(Examinee examinee : examinees){
			if(city.equals(examinee.getCity())){
				list.add(examinee);
			}
		}
		return  list;
	}
	/**
	 * 根据名字查询
	 * @param ticketId
	 * @return
	 */
	public List<Examinee> findByName(String name){
		List<Examinee>list = new ArrayList<>();
		for(Examinee examinee : examinees){
			if(name.equals(examinee.getName())){
				list.add(examinee);
			}
		}
		return  list;
	}
	/**
	 * 返回考生集合
	 * @return
	 */
	public List<Examinee> getExaminee(){
		return examinees;
	}
	/**
	 * 考生登录，登录成功，返回准考证号对应的考生对象，否则返回null
	 * @param ticketId
	 * @param password
	 * @return-
	 */
	public Examinee login(String ticketId, String password){
		for(Examinee examinee:examinees){
			if(examinee.login(ticketId, password)){
				return examinee;
			}
		}
		return null;
		
	}
	/**
	 * 根据准考证号返回考生对象，准考证号不存在返回null
	 * @param ticketld
	 * @return
	 */
	public Examinee getExamineeByTicketId(String ticketId){
		for(Examinee examinee : examinees){
			if(ticketId.equals(examinee.getTicketId())){
				return examinee;
			}
		}
		return null;	
	}
	/**
	 * 序列化对象
	 */
	public void save(){
		FileOutputStream fos = null;
		ObjectOutputStream oos = null;
		try {
			fos = new FileOutputStream(SAVE_FILE);
			oos = new ObjectOutputStream(fos);
			oos.writeObject(examinees);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 反序列化对象
	 */
	public void load(){
		FileInputStream fis = null;
		ObjectInputStream ois = null;
		try {
			fis = new FileInputStream(SAVE_FILE);
			ois = new ObjectInputStream(fis);
			examinees=(List<Examinee>) ois.readObject();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally{
			if(ois!=null){
				try {
					ois.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
	}





	



	
	

}

